package com.qo;

import lombok.Getter;
import lombok.ToString;

import java.util.Collections;
import java.util.List;

@Getter
@ToString
public class PageResult<T> {
    private Integer currentPage;   //请求传递QueryObject数据
    private Integer pageSize;      //请求传递QueryObject数据
    private List<T> list;          //数据库查询数据
    private Integer totalCount;    //数据库查询数据
    //计算得来
    private Integer prevPage;
    private Integer nextPage;
    private Integer totalPage;

    public PageResult(Integer pageSize) {
        this(1, 0, Collections.EMPTY_LIST, pageSize);
    }

    public PageResult(Integer currentPage, Integer totalCount, List<T> list, Integer pageSize) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.list = list;
        this.totalCount = totalCount;

        if (this.totalCount <= this.pageSize) {   //数据只够一页
            this.totalPage = 1;
            this.prevPage = 1;
            this.nextPage = 1;
            return;
        }

        this.totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        this.prevPage = currentPage > 1 ? currentPage - 1 : 1;
        this.nextPage = currentPage < this.totalPage ? currentPage + 1 : this.totalPage;
    }
}

